package com.yygo.webapp.controller;

import com.yygo.Constants;
import com.yygo.dao.SearchException;
import com.yygo.model.Dealer;
import com.yygo.model.User;
import com.yygo.service.DealerManager;
import com.yygo.service.UserManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ExtendedModelMap;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import java.security.Principal;


@Controller
@RequestMapping("/dealerusers*")
public class DealerUserController {
    private UserManager userManager = null;
    private DealerManager dealerManager = null;

    @Autowired
    public void setUserManager(UserManager userManager) {
        this.userManager = userManager;
    }

    @Autowired
    public void setDealerManager(DealerManager dealerManager) {
        this.dealerManager = dealerManager;
    }

    @RequestMapping(method = RequestMethod.GET)
    public ModelAndView handleRequest(Principal principal) throws Exception {
        Model model = new ExtendedModelMap();
        User user = userManager.getUserByUsername(principal.getName());
        Dealer dealer = user.getDealer();
        if (dealer != null) {
            model.addAttribute(dealerManager.getUsers(dealer.getId()));
        }

        return new ModelAndView("dealeruserList", model.asMap());
    }
}
